package com.dhnsoft.每天一道力扣;

import java.util.Arrays;

public class day04_209_长度最小的子数组 {

    public static void main(String[] args) {
        int[] nums = {12,28,83,4,25,26,25,2,25,25,25,12};
        int target = 213;
        int i = minSubArrayLen(target, nums);
        System.out.println(i);
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        if(n==0){
            return 0;
        }
        int ans = Integer.MAX_VALUE;
        int start = 0;
        int end = 0;
        int sum = 0;
        while (end<n){
            sum += nums[end];
            while (sum>=target){
                ans = Integer.min(ans,end-start+1);
                sum-=nums[start];
                start++;
            }
            end++;
        }
        return ans == Integer.MAX_VALUE?0:ans;
    }
}
